package com.wude.guild.fragment;

import android.annotation.SuppressLint;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.mc.developmentkit.utils.ToastUtil;
import com.umeng.analytics.MobclickAgent;
import com.wude.bean.AboutUs;
import com.wude.guild.R;
import com.wude.guild.WebActivity;
import com.wude.guild.servercore.mints.TitleBaseFragment;
import com.wude.http.HttpCom;
import com.wude.http.HttpUtils;
import com.wude.tools.DbUtils;
import com.wude.tools.Utils;

import org.xutils.DbManager;
import org.xutils.ex.DbException;
import org.xutils.x;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by Administrator on 2017/11/23 0023.
 * <p>
 * 关于我们
 */

public class AboutUsFragment extends TitleBaseFragment {
    private Unbinder unbinder;
    @BindView(R.id.appicon)
    ImageView appicon;
    @BindView(R.id.appname)
    TextView appname;
    @BindView(R.id.gongzh)
    TextView gongzh;
    @BindView(R.id.wanjiaqun)
    TextView wanjiaqun;
    @BindView(R.id.qq)
    TextView qq;
    @BindView(R.id.wangzhan)
    TextView wangzhan;

    private AboutUs about;

    @Override
    protected View createView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.about_us_fragment, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        unbinder = ButterKnife.bind(this, view);
        setHeaderTitle("关于我们");

        initdata();
        HttpCom.POST(handler, HttpCom.getVisonURL(), null, false);
    }

    private void initdata() {
        DbManager db = DbUtils.getDB();
        try {
            AboutUs byId = db.findById(AboutUs.class, 2);
            if (byId != null) {
                Utils.Fill(appicon, byId.iconUrl);
                appname.setText(byId.appname);
                gongzh.setText(byId.gongzhonghao);
                qq.setText(byId.kefuQQ);
                wanjiaqun.setText(byId.wanjiaqun);
            }
        } catch (DbException e) {
            e.printStackTrace();
        }
    }

    @SuppressLint("HandlerLeak")
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    about = HttpUtils.DNSAboutUs(msg.obj.toString());
                    if (about != null) {
                        Utils.Fill(appicon, about.iconUrl);
                        appname.setText(about.appname);
                        gongzh.setText(about.gongzhonghao);
                        qq.setText(about.kefuQQ);
                        wangzhan.setText(about.guanwang);
                        wanjiaqun.setText(about.wanjiaqun);
                    }
                    break;
                case 2:

                    break;
            }
        }
    };

    @Override
    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("AboutUsFragment");
    }

    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("AboutUsFragment");
    }

    /****************
     * 发起添加群流程。群号：牛擦协会(125638122) 的 key 为： HD4wt8XxGOIXKUHrvs0wK-d_PPMywLHI
     * 调用 joinQQGroup(HD4wt8XxGOIXKUHrvs0wK-d_PPMywLHI) 即可发起手Q客户端申请加群 牛擦协会(125638122)
     *
     * @param key 由官网生成的key
     * @return 返回true表示呼起手Q成功，返回fals表示呼起失败
     ******************/
    public boolean joinQQGroup(String key) {
        Intent intent = new Intent();
        intent.setData(Uri.parse("mqqopensdkapi://bizAgent/qm/qr?url=http%3A%2F%2Fqm.qq.com%2Fcgi-bin%2Fqm%2Fqr%3Ffrom%3Dapp%26p%3Dandroid%26k%3D" + key));
        // 此Flag可根据具体产品需要自定义，如设置，则在加群界面按返回，返回手Q主界面，不设置，按返回会返回到呼起产品界面    //intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
        try {
            startActivity(intent);
            return true;
        } catch (Exception e) {
            // 未安装手Q或安装的版本不支持
            ToastUtil.showToast("未安装手Q或安装的版本不支持");
            return false;
        }
    }


    /**
     * 一键唤起QQ聊天界面
     *
     * @param context
     * @param packageName
     * @return
     */
    public boolean checkApkExist(Context context, String packageName) {
        if (packageName == null || "".equals(packageName))
            return false;
        try {
            ApplicationInfo info = context.getPackageManager().getApplicationInfo(packageName,
                    PackageManager.GET_UNINSTALLED_PACKAGES);
            return true;
        } catch (PackageManager.NameNotFoundException e) {
            return false;
        }
    }

    @OnClick({R.id.gzh_lay, R.id.qun_lay, R.id.kefu_lay, R.id.wangzhan_lay})
    public void onClick(View view) {
        if (about == null) {
            ToastUtil.showToast("aboutbean为空");
            return;
        }
        switch (view.getId()) {
            case R.id.gzh_lay:                                      //微信公众号
                ClipboardManager cmb = (ClipboardManager) x.app().getSystemService(Context.CLIPBOARD_SERVICE);
                cmb.setText(about.gongzhonghao);
                ToastUtil.showToast("复制完成");
                break;
            case R.id.qun_lay:                                      //玩家群
//                if (about.wanjiaqunkey != null && !about.wanjiaqunkey.equals("")) {
//                    joinQQGroup(about.wanjiaqunkey);
//                } else {
//                    ToastUtil.showToast("群key为空");
//                }
//                break;
            case R.id.kefu_lay:                                         //客服QQ
//                if (checkApkExist(getContext(), "com.tencent.mobileqq")) {
//                    startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("mqqwpa://im/chat?chat_type=wpa&uin=" + about.kefuQQ + "&version=1")));
//                } else {
//                    ToastUtil.showToast("本机未安装QQ应用");
//                }
                break;
            case R.id.wangzhan_lay:                                     //官网
                Intent intent = new Intent(getContext(), WebActivity.class);
                intent.putExtra("url", about.guanwang);
                intent.putExtra("name", "官网");
                startActivity(intent);
                break;
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
